// 简化的安全服务层核心实现

#![forbid(unsafe_code)]

use std::sync::{Arc, Mutex};
use log::{info, warn};

// 统一错误类型
#[derive(Debug)]
pub enum ServiceError {
    MemoryError(String),
    ProcessError(String),
    FileSystemError(String),
    ConfigurationError(String),
    GenericError(String),
}

impl std::fmt::Display for ServiceError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ServiceError::MemoryError(msg) => write!(f, "Memory Error: {}", msg),
            ServiceError::ProcessError(msg) => write!(f, "Process Error: {}", msg),
            ServiceError::FileSystemError(msg) => write!(f, "File System Error: {}", msg),
            ServiceError::ConfigurationError(msg) => write!(f, "Configuration Error: {}", msg),
            ServiceError::GenericError(msg) => write!(f, "Generic Error: {}", msg),
        }
    }
}

impl std::error::Error for ServiceError {}

// 基础服务特质
pub trait BaseService {
    fn start(&mut self) -> Result<(), ServiceError>;
    fn stop(&mut self) -> Result<(), ServiceError>;
    fn status(&self) -> ServiceStatus;
}

// 服务状态
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServiceStatus {
    Stopped,
    Starting,
    Running,
    Stopping,
    Error,
}

// 简化的服务管理器
pub struct SimpleServiceManager {
    memory_service: Arc<Mutex<MemoryService>>,
    process_service: Arc<Mutex<ProcessService>>,
    file_system_service: Arc<Mutex<FileSystemService>>,
    initialized: bool,
}

impl SimpleServiceManager {
    pub fn new() -> Result<Self, ServiceError> {
        info!("Initializing Simple Service Manager");
        
        let memory_service = Arc::new(Mutex::new(
            MemoryService::new().map_err(|e| ServiceError::MemoryError(e.to_string()))?
        ));
        
        let process_service = Arc::new(Mutex::new(
            ProcessService::new().map_err(|e| ServiceError::ProcessError(e.to_string()))?
        ));
        
        let file_system_service = Arc::new(Mutex::new(
            FileSystemService::new().map_err(|e| ServiceError::FileSystemError(e.to_string()))?
        ));

        Ok(SimpleServiceManager {
            memory_service,
            process_service,
            file_system_service,
            initialized: false,
        })
    }
    
    pub fn start_all(&mut self) -> Result<(), ServiceError> {
        if self.initialized {
            return Err(ServiceError::GenericError("Services already started".to_string()));
        }
        
        info!("Starting all services");
        
        // 启动内存服务
        self.memory_service.lock().unwrap().start()?;
        
        // 启动进程服务
        self.process_service.lock().unwrap().start()?;
        
        // 启动文件系统服务
        self.file_system_service.lock().unwrap().start()?;
        
        self.initialized = true;
        info!("All services started successfully");
        Ok(())
    }
    
    pub fn stop_all(&mut self) -> Result<(), ServiceError> {
        if !self.initialized {
            return Ok(());
        }
        
        info!("Stopping all services");
        
        // 停止文件系统服务
        if let Err(e) = self.file_system_service.lock().unwrap().stop() {
            warn!("Error stopping file system service: {}", e);
        }
        
        // 停止进程服务
        if let Err(e) = self.process_service.lock().unwrap().stop() {
            warn!("Error stopping process service: {}", e);
        }
        
        // 停止内存服务
        if let Err(e) = self.memory_service.lock().unwrap().stop() {
            warn!("Error stopping memory service: {}", e);
        }
        
        self.initialized = false;
        info!("All services stopped");
        Ok(())
    }
}

// 导入简化的服务模块
pub mod memory_service;
pub mod process_service;
pub mod file_system_service;
pub mod config;

// 重新导出服务类型
pub use memory_service::MemoryService;
pub use process_service::ProcessService;
pub use file_system_service::FileSystemService;
pub use config::SimpleConfig;

impl Drop for SimpleServiceManager {
    fn drop(&mut self) {
        let _ = self.stop_all();
    }
}